
import java.util.*;
import javax.swing.*;

public class UtilityWorker {
	/**
	 * this class has the utility methods i am going to need its doIt method
	 * will read a list containing the commands in ~my~ format and executes the
	 * commands.
	 */
	public static void updateText(JTextArea a, JButton c) {
		String s = a.getText();
		s += c.getText();
		s += "\nLOGO>";
		a.setText(s);
	}

	public static void doIt(DrawArea dw, java.util.List ll) {
		String s = (String) ll.get(0);
		s = s.toLowerCase();
		if (s.equals("repeat")) {
			int i = Integer.parseInt((String) ll.get(1));
			System.out.println(i);
			ll.remove(0);
			ll.remove(0);
			for (int j = 0; j < i; j++) {
				ll = doIt1(dw, ll);
			}
		} else {
			doIt1(dw, ll);
		}
	}

	private static List doIt1(DrawArea dw, java.util.List l)
	//throws ArrayIndexOutOfBoundsException
	{
		System.out.println(l);
		List temp = new ArrayList(l);
		Iterator i = l.iterator();
		while (i.hasNext()) {
			String s = (String) l.get(0);
			s = s.toLowerCase();
			if (s.equals("fd") || s.equals("forward")) {
				System.out.println("fd");
				dw.forward(Integer.parseInt((String) l.get(1)));
				i.next();
				i.remove();
				i.next();
				i.remove();
			} else if (s.equals("bk") || s.equals("backward")) {
				dw.backward(Integer.parseInt((String) l.get(1)));
				i.next();
				i.remove();
				i.next();
				i.remove();
			} else if (s.equals("rt") || s.equals("rightturn")) {
				dw.rightTurn(Integer.parseInt((String) l.get(1)));
				i.next();
				i.remove();
				i.next();
				i.remove();
			} else if (s.equals("lt") || s.equals("leftturn")) {
				dw.leftTurn(Integer.parseInt((String) l.get(1)));
				i.next();
				i.remove();
				i.next();
				i.remove();
			} else if (s.equals("pu") || s.equals("penup")) {
				dw.penUp();
				i.next();
				i.remove();
			} else if (s.equals("pd") || s.equals("pendown")) {
				dw.penDown();
				i.next();
				i.remove();
			} else if (s.equals("pe") || s.equals("penerase")) {
				dw.penErase();
				i.next();
				i.remove();
			} else if (s.equals("home")) {
				dw.home();
				i.next();
				i.remove();
			} else if (s.equals("cls")) {
				System.out.println("cls");
				dw.makeBlank();
				dw.repaint();
				i.next();
				i.remove();
				System.out.println(l);
			} else {
				i.next();
				i.remove();
			}
		}
		return temp;
	}

	public static void main(String[] args) {
		DrawArea d = new DrawArea();
		List l;//=new ArrayList();
		String s = "repeat 4(fd 10 rt 90)";
		l = StringWorker.lastCommandArr(s);
		doIt(d, l);
	}
}